//
//  FWBC.m
//  UniverseProject
//
//  Created by king on 2021/6/23.
//  Copyright © 2021 Cloud2209. All rights reserved.
//

#import "LGWeb.h"
#import "FWBC.h"

@interface FWBC ()<LGWebDelegate>

@property (nonatomic, weak)UIScrollView *scrollView;
@property (nonatomic, weak)LGWeb *activityWebView;

@end

@implementation FWBC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = LGCOLOR(@"#F8F8F8");
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

//    [self method1];
    [self method2];
    
}

- (void)method2{
    NSError    *error=nil;
    // 通过指定的路径读取文本内容
    NSString *content = [NSString    stringWithContentsOfFile:[[NSBundle    mainBundle]    pathForResource:@"file_str"ofType:@"txt"]    encoding:NSUTF8StringEncoding    error:&error];
    [self createWebView];
    [self.activityWebView avtivityLoadWithContent:content];
}

- (void)method1{
    


    CGFloat detailY = 0;
    NSError    *error=nil;

    // 通过指定的路径读取文本内容
    NSString    *str = [NSString    stringWithContentsOfFile:[[NSBundle    mainBundle]    pathForResource:@"file_str"ofType:@"txt"]    encoding:NSUTF8StringEncoding    error:&error];
//    NSLog(@"%@",str);

    UITextView *detailView = [[UITextView alloc]initWithFrame:CGRectMake(0, detailY, K_SCREEN_WIDTH, 0)];
    detailView.scrollEnabled = NO;
    detailView.editable = NO;
    [self.scrollView addSubview:detailView];
    
    [self.view showIndicator];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSAttributedString *attributedString = [self setAttributedString:str font:LGFONT(16) lineSpacing:10];
        CGFloat textHeight = [self getHTMLHeightByStr:str font:LGFONT(16) lineSpacing:10 width:K_SCREEN_WIDTH];
        dispatch_async(dispatch_get_main_queue(), ^{
            detailView.attributedText = attributedString;
            detailView.height = textHeight;
            [self.view hideIndicator];
        });
    });

    
}


- (void)createWebView{

    CGFloat padding = 15;
    CGFloat maxW = K_SCREEN_WIDTH - padding * 2;
    //答案
    LGWeb *webC = [[LGWeb alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 0)];
    webC.webView.scrollView.bounces = NO;
    webC.webView.scrollView.scrollEnabled = NO;
    webC.tag = 0;
    webC.userInteractionEnabled = NO;
    webC.webdelegate = self;
    webC.heightFix = YES;
    [self.scrollView addSubview:webC];
    self.activityWebView = webC;

}


- (void)webView:(WKWebView *)webView LoadResult:(BOOL)result webH:(CGFloat)webH{
    self.activityWebView.height = webH;
    self.scrollView.contentSize = CGSizeMake(0, webH + 100);
}

/**
 html 富文本设置

 @param str html 未处理的字符串
 @param font 设置字体
 @param lineSpacing 设置行高
 @return 默认不将 \n替换<br/> 返回处理好的富文本
 */
-(NSMutableAttributedString *)setAttributedString:(NSString *)str font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing
{
    //如果有换行，把\n替换成<br/>
    //如果有需要把换行加上
//    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
    //设置HTML图片的宽度
    str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str];
    NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
    //设置富文本字的大小
    [htmlString addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, htmlString.length)];
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [htmlString length])];
    
    return htmlString;

}



/**
 计算html字符串高度

 @param str html 未处理的字符串
 @param font 字体设置
 @param lineSpacing 行高设置
 @param width 容器宽度设置
 @return 富文本高度
 */
-(CGFloat )getHTMLHeightByStr:(NSString *)str font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing width:(CGFloat)width
{
//    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
    str = [NSString stringWithFormat:@"<head><style>img{width:%f !important;height:auto}</style></head>%@",[UIScreen mainScreen].bounds.size.width,str];
    
    NSMutableAttributedString *htmlString =[[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:NULL error:nil];
    [htmlString addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, htmlString.length)];
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:lineSpacing];
    [htmlString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [htmlString length])];
    
    CGSize contextSize = [htmlString boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    return contextSize.height ;
}

@end
